const CRON_TIME_CN = ["秒", "分钟", "小时", "天", "月", "周", "年"]
const HOURS = 24;
const TIMESCALE = 60;
export function translateToChinese(cronExp, cronTimeArr) {
    if(!cronTimeArr){
        cronTimeArr = CRON_TIME_CN;
    }
    if (cronExp == null || cronExp.length < 1) {
        return ""
    }

    let tmpCorns = cronExp.split(" ");
    let sBuffer = ""
    if (tmpCorns.length === 6 || tmpCorns.length === 7) {
        // if (tmpCorns.length === 7) {
        //     //解析年 year
        //     let year = tmpCorns[6];
        //     if (year !=="*" && year !=="?") {
        //         sBuffer = sBuffer.concat(year).concat(cronTimeArr[6])
        //     }
        // }

        //解析月 month
        let months = tmpCorns[4];
        if (months !=="*" && months !=="?") {
            if (months.includes("/")) {
                sBuffer = sBuffer.concat("从").concat(months.split("/")[0]).concat("月开始").concat("，每").concat(months.split("/")[1]).concat(cronTimeArr[4])
            } else {
                sBuffer = sBuffer.concat("每年").concat(months).concat(cronTimeArr[4])
            }
        }

        //解析周 DayOfWeek
        let dayOfWeek = tmpCorns[5];
        if (dayOfWeek !=="*" && dayOfWeek !=="?") {
            if (dayOfWeek.includes(",")) {//多个数字，逗号隔开
                sBuffer = sBuffer.concat("每周的第").concat(dayOfWeek).concat(cronTimeArr[3])
            } else if (dayOfWeek.includes("L") && dayOfWeek.length > 1) {
                let weekNum = dayOfWeek.split("L")[0];
                let weekName = judgeWeek(weekNum);
                sBuffer = sBuffer.concat("本月的最后一个");
                sBuffer = sBuffer.concat(weekName)
            } else if (dayOfWeek.includes("-")) {
                let splitWeek = dayOfWeek.split("-");
                let weekOne = judgeWeek(splitWeek[0]);
                let weekTwo = judgeWeek(splitWeek[1]);
                sBuffer = sBuffer.concat("每周的").concat(weekOne).concat("到").concat(weekTwo)
            } else if (dayOfWeek.includes("#")) {
                let splitWeek = dayOfWeek.split("#");
                let weekTwo = judgeWeek(splitWeek[1]);
                sBuffer = sBuffer.concat("本月第").concat(splitWeek[0]).concat("周的").concat(weekTwo)
            } else if (dayOfWeek.includes("/")) {
                let splitWeek = judgeWeek(dayOfWeek.split("/")[0]);
                let weekTwo = dayOfWeek.split("/")[1];
                sBuffer = sBuffer.concat("从").concat(splitWeek).concat("开始，每").concat(weekTwo).concat("天执行一次")
            } else {
                if ("L" === dayOfWeek) {
                    dayOfWeek = "7";
                }

                let weekNums = +dayOfWeek;
                if (weekNums < 0 || weekNums > 7) {
                    return "cron表达式有误, dayOfWeek数字应为1-7"
                }
                sBuffer = sBuffer.concat("每周的");
                let weekName = judgeWeek(dayOfWeek);
                sBuffer = sBuffer.concat(weekName)
            }
        }


        let days = tmpCorns[3];
        if (days !== "?" && days !== "*") {
            if (dayOfWeek.includes("/")) {
                sBuffer = sBuffer.concat("每周从第").concat(days.split("/")[0]).concat("天开始").concat("，每").concat(days.split("/")[1]).concat(cronTimeArr[3])
            } else if ("L" === days) { //处理L 每月的最后一天
                sBuffer = sBuffer.concat("每月的最后一天")
            } else if ("W" === days) { //处理W
                sBuffer = sBuffer.concat(days)
            } else if (days.includes("#")) {
                let splitDays = days.split("#");
                let weekNum = splitDays[0]; //前面数字表示周几
                let weekOfMonth = splitDays[1]; //后面的数字表示第几周 范围1-4 一个月最多4周
                let weekName = judgeWeek(weekNum);
                sBuffer = sBuffer.concat("每月第").concat(weekOfMonth).concat(cronTimeArr[5]).concat(weekName);
            } else {
                sBuffer = sBuffer.concat("每月第").concat(days).concat(cronTimeArr[3]);
            }
        } else {
            if (sBuffer.toString().length === 0 || tmpCorns.length === 7) {
                sBuffer = sBuffer.concat("每").concat(cronTimeArr[3]);
            }
        }

        let hours = tmpCorns[2];
        if (hours !== "*") {
            if (hours.includes("/")) {
                sBuffer = sBuffer.concat(appendGapInfo(hours, HOURS, 2));
            } else {
                if (!(sBuffer.toString().length > 0)) {
                    sBuffer = sBuffer.concat("每天").concat(hours).concat(cronTimeArr[2]);
                } else {
                    sBuffer = sBuffer.concat(hours).concat(cronTimeArr[2])
                }
            }
        }

        //解析分
        let minutes = tmpCorns[1];
        if (minutes !== "*") {
            if (minutes.includes("/")) {
                sBuffer = sBuffer.concat(appendGapInfo(minutes, TIMESCALE, 1));
            } else if (minutes === "0") {

            } else if (minutes.includes("-")) {
                let splitMinute = minutes.split("-");
                sBuffer = sBuffer.concat(splitMinute[0]).concat(cronTimeArr[1]).concat("到").append(splitMinute[1]).concat(cronTimeArr[1]).concat("每分钟")
            } else {
                sBuffer = sBuffer.concat(minutes).concat(cronTimeArr[1])
            }
        }

        //解析秒
        let seconds = tmpCorns[0];
        if (seconds !== "*") {
            if (seconds.includes("/")) {
                sBuffer = sBuffer.concat(appendGapInfo(seconds, TIMESCALE, 0))
            } else if (seconds !== "0") {
                sBuffer = sBuffer.concat(seconds).concat(cronTimeArr[0])
            }
        }

        if (sBuffer.toString().length > 0) {
            sBuffer = sBuffer.concat("执行一次")
        } else {
            sBuffer = sBuffer.concat("表达式中文转换异常")
        }
    }

    return sBuffer.toString()
}

export function judgeWeek(weekNum) {
    let weekName = matchNameCn(weekNum.toString());
    let weekNums = +weekNum;
    if (weekNums < 0 || weekNums > 7) {
        return "cron表达式有误，dayOfWeek数字应为1-7"
    }
    return weekName !== "" ? weekName : weekNum.toString();
}

export function appendGapInfo(time, rangeNum, index) {
    let sBufferTemp = "";
    let splitTime = time.split("/");
    let startNum = splitTime[0];
    let gapNum = splitTime[1];
    let endNum = rangeNum + +startNum - +gapNum;
    let endStr = endNum.toString();
    let timeUnit = CRON_TIME_CN[index];
    sBufferTemp = sBufferTemp.concat("从").concat(startNum).concat(timeUnit).concat("开始")
        // .concat("到").concat(endStr).concat(timeUnit).concat("范围内")
        .concat("，每隔").concat(gapNum).concat(timeUnit);
    return sBufferTemp.toString();
}

let weekEnum = ["星期天", "星期一","星期二", "星期三", "星期四", "星期五","星期六"]
export function matchNameCn(code) {
    return weekEnum[code-1]
}